﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//using Modbus.Device;
//using Modbus.Utility;
using MySql.Data.MySqlClient;

namespace TJ_MainNameSpace
{
    public partial class Peizhi1 : Form
    {
        public Peizhi1()
        {
            InitializeComponent();
        }

        //MySqlConnection con = new MySqlConnection(new MySQLConnectionString(TongJi_EMS.Properties.Settings.Default["server"].ToString(), TongJi_EMS.Properties.Settings.Default["database"].ToString(), TongJi_EMS.Properties.Settings.Default["mysqluser"].ToString(), TongJi_EMS.Properties.Settings.Default["mysqlpassword"].ToString(), shujuyuan3.port).AsString);
        MySqlConnection con = new MySqlConnection(TongJi_EMS.Properties.Settings.Default.monitorConnection);

        public static string name = "";
        public static string new_name = "";
        public static string address = "";
        public static string protocol = "";
        public static string outtime = "";
        public static string area_name = "";

        private void button2_Click(object sender, EventArgs e)
        {
            new_name = this.eqpname.Text;
            address = this.textBox1.Text;
            outtime = this.textBox3.Text;
            area_name = this.comboBox4.Text;
            if (this.eqpname.Text == "")
            { MessageBox.Show("设备名不能为空"); }
            else if (this.textBox1.Text == "")
            { MessageBox.Show("必须指定设备地址"); }
            else
            {
                if (For_shebei.ifalart == false)
                {
                    try
                    {
                        con.Open();
                        //数据库重新设计后的最新更改
                        string mysql = "SELECT   id FROM  device where  project='" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'AND (slaver_address='" + Peizhi1.address + "'OR device.name='" + name + "')";
                        //老代码
                        //string mysql = "SELECT   id FROM  device where  project='" + FormStart.Gongcheng_Name + "'AND (slaverid='" + Peizhi1.address + "'OR device.name='"+name+"')";
                        MySqlCommand Cmd2 = new MySqlCommand(mysql, con);
                        object count = Cmd2.ExecuteScalar();
                        con.Close();
                        if (count == null)
                        {
                            Peizhi2 a = new Peizhi2();
                            a.Show();
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show("设备名称或设备地址已被使用！ ");
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
                else
                {
                    Peizhi2 a = new Peizhi2();
                    a.Show();
                    this.Close();
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void Peizhi1_Load(object sender, EventArgs e)
        {
            this.eqpname.Text = name;
            this.textBox1.Text = address;
            #region 数据库重新设计后的最新更改
            MySqlConnection con = new MySqlConnection(TongJi_EMS.Properties.Settings.Default.monitorConnection);
            con.Open();
            MySqlCommand cmd = new MySqlCommand("select area_name from dbarea where project ='" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'", con);
            MySqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                this.comboBox4.Items.Add(reader.GetValue(0).ToString());
            }
            con.Close();
            this.comboBox4.SelectedIndex = this.comboBox4.FindString(Peizhi1.area_name);
            #endregion
        }

    }
}